<?php
/**
 * @version			$Id$
 * @author			Richard Asp
 * @package			Joomla
 * @subpackage		gnudoor
 * @copyright		(C) 2010 gnudoor LLC
 * @license      GNU/GPL, see LICENSE.php
 * Joomla! is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 * See COPYRIGHT.php for copyright notices and details.
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.application.component.view');

class GnudoorViewProviders extends JView
{
    function display($tpl = null)
    {
    	global $mainframe, $option;
    	$userid =& JFactory::getUser();
		$grouparray = $this->get('GnudoorGroup');

		foreach ($grouparray as $user) {
			if ( $user->userid == $userid->id ) {
				$g = $user->gnudoor_group;
			}
		}

		if ($g != 'provider') {
			/*
			 * All non-providers get redirected
			 */
			JApplication::redirect('index.php?option=com_gnudoor&controller=noaccess');
		}
		$attributes =& $this->get('AttributeArray');
		$this->assignRef( 'titles', $attributes);
		
		if (JRequest::getWord( 'task' ) != 'takeownership') {
			/*
			 * This goes to the List
			 */
			/* Call the state object */
	        $state =& $this->get( 'state' );
	        $lists['active_tab']= $state->get('active_tab');
			
			$rows = $this->get('data');
			$array = array();
			foreach ($rows as $row) {
				/*
				 * This section formats the date
				 */
				$dateformat = strftime('%b %d %Y', strtotime($row->dateAdded));
				$row->dateAdded = $dateformat;
				$nextstepgif = JHTML::image('components/com_gnudoor/assets/img/BlueArrowsRight16x17.gif','Take', 'title="Take Ownership" width="16" height="16"');
				$nexturl = 'index.php?option=com_gnudoor&controller=providers&id=' . $row->id . '&task=takeownership';
				$nextlink = JHTML::link($nexturl,$nextstepgif);		
				$row->tolink = $nextlink;
			    if ($row->dateDue == 0) {
			    	$row->dateDue = JText::_('GNUDOOR_PROVIDER_NOT_SET');
			    } else {
			    	$format = '%m/%d/%y %H:%M %p';
			    	$timest = strtotime($row->dateDue); 
			    	$row->dateDue = strftime($format,$timest);
			    }
				if ($row->providerid == 0) {
			    	$row->providerid = JText::_('GNUDOOR_PROVIDER_NOT_ASSIGNED');
			    } else {
					$model = $this->getModel('Providers');
			    	$row->userid = $model->getUserName($row->userid)->name;
			    } 
			    array_push($array, $row);
			}
	
			$thislink = JRoute::_('index.php?option=com_gnudoor&controller=providers&active_tab=tab_all');
			$this2link = JRoute::_('index.php?option=com_gnudoor&controller=providers&active_tab=tab_one');
	
			$text1 = JText::_('GNUDOOR_PROVIDER_ALL');
			$text2 = JText::_('GNUDOOR_PROVIDER_MINE');
	
			switch ($this->lists['active_tab'])  {
		    	case 'tab_all' :
		    		$class1 = 'here';
		    		$class2 = '';
		    		break;
		    	case 'tab_one' :
		    		$class1 = '';
		    		$class2 = 'here';
		    		break;
		    	default;
		    }
	
			$this->assignRef( 'rows', 	$array );
		    
		    $this->assignRef( 'lists', 	$lists );
		    $this->assignRef( 'link1', 	$thislink );
			$this->assignRef( 'link2', 	$this2link );
		    $this->assignRef( 'text1', 	$text1 );
			$this->assignRef( 'text2', 	$text2 );
		    $this->assignRef( 'class1', $class1 );
			$this->assignRef( 'class2', $class2 );
		} else {
			/*
			 * This goes to the record page
			 */
			$cid = JRequest::getInt( 'id' );
			$model = &$this->getModel();
			$row = $model->getRecord($cid);

			$this->assignRef( 'row', 	$row );
		}
		$host_articles_base = JRoute::_(JURI::base() . 'index2.php?option=com_content&view=article&id=');
		$this->assignRef('articlebase', $host_articles_base);
		
		$this->assignRef( 'userid', 	$userid );		
		parent::display($tpl);
    }
}


?>